let express = require("express")();
let mysql = require("mysql")
let url = require("url");
let fs = require("fs");
let expressStatic = require('express-static');
const port = 81;
var sql = mysql.createConnection({
	// 别改 
	port: 3307,
	host: 'localhost',
	user: 'root',
	password: '123456',
	database: '2006'
});
sql.connect();

express.get("/", (request, response) => {
	fs.readFile("nanvn.html", (error, data) => {
		if (error) {
			response.end("error");
			console.log("找不到首页文件了")
		} else {
			response.end(data);
		}
	})
})

express.get("/heihei", (request, response) => {
	sql.query(`SELECT * FROM students`, (error, data) => {
		if (error) {
			response.end("database error")
			console.log("数据查找失败")
		} else {
			response.end(JSON.stringify(data))
		}
	})
})


express.get("/nana", (request, response) => {
	let name = url.parse(request.url, true).query.name
	let age = url.parse(request.url, true).query.age
	let sex = url.parse(request.url, true).query.sex
	let city = url.parse(request.url, true).query.city
	sql.query(`INSERT INTO students (name,age,sex,city) VALUES ("${name}","${age}","${sex}","${city}")`, (error, data) => {
		if (error) {
			response.end("database error");
			console.log("添加失败")
		} else {
			response.end("success")
		}
	})
})


// express.get("/xiugai", (request, response) => {
// 	let name = url.parse(request.url, true).query.name
// 	let age = url.parse(request.url, true).query.age
// 	let sex = url.parse(request.url, true).query.sex
// 	let city = url.parse(request.url, true).query.city
// 	sql.query(`UPDATE students SET (name="${name}",age="${age}",sex="${sex}",city=$"{city}") WHERE (id="${id}")`, (error,
// 		data) => {
// 		if (error) {
// 			response.end("database error")
// 			console.log("修改失败")
// 		} else {
// 			response.end("success")
// 			console.log("修改成功")
// 		}
// 	})
// })

express.get("/del", (request, response) => {
	let id = url.parse(request.url, true).query.id
	sql.query(`DELETE FROM students WHERE id=${id}`, (error, data) => {
		if (error) {
			response.end("database error")
			console.log("删除失败")
		} else {
			response.end("success")
			console.log("删除成功")
		}
	})
})




express.get("/tianjia", (request, response) => {
	let name = url.parse(request.url, true).query.name
	let password = url.parse(request.url, true).query.password
	sql.query(`SELECT * FROM users WHERE (name="${name}")`, (error, data) => {

			if (data.length) {
				response("requst")
				console.log("requst")
			} else {
				sql.query(`INSERT INTO users (name,password) VALUES ("${name}","${password}")`, (error, data) => {
					if (error) {
						response.end("database error");
						console.log("添加失败")
					} else {
						response.end("success")
					}
				})

			}
	})
})


express.get("/denglu", (request, response) => {
	let name = url.parse(request.url, true).query.name
	let password = url.parse(request.url, true).query.password
	sql.query(`SELECT * FROM users WHERE (name="${name}")`, (error, data) => {
		console.log(error)
		if (error) {
			response.end("database error")
			console.log("登录失败")
		} else {
			response.end("success")
			console.log("登录成功")
		}
	})
})



express.use(expressStatic(`${__dirname}`))
express.listen(port, () => {
	console.log(`server is running at ${port}`)
})
